import Acceleration2D from "./Acceleration2D";
import Speed2D from "./Speed2D";

export default class Movement2D {

    /**
     *
     * @param speed {number=1}
     * @param angle {number=0}
     * @param maxSpeed {number=10}
     */
    constructor({speed = 1, angle = 0, maxSpeed = 10}) {
        this.speed2D = new Speed2D(speed, angle);
        this.accelerationMap = {};
        this.maxSpeed = maxSpeed;
        Object.defineProperties(this, {
            speed: {
                get: () => {
                    return this.speed2D.speed;
                },
                set: (speed) => {
                    this.speed2D.speed = speed;
                }
            },
            angle: {
                get: () => {
                    return this.speed2D.angle;
                },
                set: (angle) => {
                    this.speed2D.angle = angle;
                }
            }
        });
    }

    applyAcceleration(key, acceleration) {
        this.accelerationMap[key] = acceleration;
        return this;
    }

    clearAcceleration(key) {
        delete this.accelerationMap[key];
        return this;
    }

    clearAllAcceleration(key) {
        this.accelerationMap = {};
        return this;
    }

    getAcceleration(key) {
        return this.accelerationMap[key];
    }

    /**
     * @param point {Point2D}
     */
    move(point) {
        let acceleration = new Acceleration2D();
        Object.keys(this.accelerationMap).forEach(key => {
            acceleration.mergeAcceleration(this.accelerationMap[key], point, this);
        });
        this.speed2D.verticalSpeed += acceleration.verticalAcceleration;
        this.speed2D.horizontalSpeed += acceleration.horizontalAcceleration;
        if (this.speed >= this.maxSpeed) {
            this.speed = this.maxSpeed;
        }
        let newPoint = point['getOtherPointFromDistanceAndDeg'](this.speed, this.angle);
        point['x'] = newPoint.x;
        point['y'] = newPoint.y;
    }

}